package com.server.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class HttpUtils {

	public static String GetHttpData(String url,String param,String Method ){
		PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestMethod(Method);
            if(Method.equals("POST")){
            	// 发送POST请求必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(conn.getOutputStream());
                // 发送请求参数
                if(!"".equals(param)){
                	out.print(param);
                }
                
                // flush输出流的缓冲
                out.flush();
            }
            
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;

            while ((line = in.readLine()) != null) {
                result += line;
            }

        } catch (Exception e) {
            System.out.println("发送 HTTP 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
		
	}
	
	
	//

    public static void main( String[] args ) throws Exception
    {
    	String public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAybpowNJjQVv4+ketkyWjr7EXaBXbfpJByBXpXaWHJcVrmab4Goe2Hj+pu6M1vHVbdECfdCPJFuXS/m2I4B9Vj89/txCp+GoXC9dpzOOdTXVL+iagikT1U+Jaz0sMWSi9SLkXq8KZDu/SYLsdbVhX/vDtXJm2CbGYsOzCklLIoKnJi+cr6XZjOljGWzzwQgMH2nlPXWpPfs8ug8J0vPuJSW/B8aCetJ30w8d1AxAgw1W3OU+LQOZ9uz/vgHhKMAj/+YqoMhdprCBAjgSDrX4l71wxzkdCxv35/tEBE4aBJSgdm3x6YjshXDic3i/GtbOpMvAbOwrrNpuNESZvOmviwwIDAQAB";
    	
    	System.out.println(RSAUtils.encryptedDataOnJava("2", RSAUtils.PUBLICKEY));
    	
    	long timestamp = System.currentTimeMillis()/1000;
    	String url = "https://xs2152git.abc123rt.com/Api/Interface/publicapi/login";
    	Map<String, Object> data = new HashMap<String, Object>();
    	data.put("timestamp", timestamp);
    	data.put("logintype", "");
    	data.put("account", "100");
    	data.put("loginpass", RSAUtils.encryptedDataOnJava("123123", public_key));
    	data.put("sign", "");
    	String temp = RSASignature.sign(JSONObject.toJSONString(data), RSAUtils.PRIVATEKEY);
    	data.put("sign", temp);
    	
    	temp = JSONObject.toJSONString(data);
    	
    	Map<String, Object> data_2 = new HashMap<String, Object>();
    	data_2.put("timestamp", timestamp);
    	data_2.put("encryptdata", "ASDFUIEDXSAESCHD" + RSAUtils.iv + RSAUtils.AESEncrypt(temp));
		String s = GetHttpData(url,JSONObject.toJSONString(data_2),"POST");

    	System.out.println(s);
    	
    	
    	//String url = "https://";
    	//GetHttpData(url,"","POST");
    	//System.out.println(RSAUtils.encryptedDataOnJava("hello", public_key));
    }
}
